package top.sdwu_top.kbase.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import top.sdwu_top.kbase.common.aspect.exception.MyException;
import top.sdwu_top.kbase.entity.KbaseSysDict;
import top.sdwu_top.kbase.mapper.KbaseSysDictMapper;
import top.sdwu_top.kbase.service.IKbaseSysDictService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import top.sdwu_top.kbase.vo.KbaseSysDictVo;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 字典表 服务实现类
 * </p>
 *
 * @author zhipeng.duan@nvr-china.com
 * @since 2020-02-20
 */
@Service
public class KbaseSysDictServiceImpl extends ServiceImpl<KbaseSysDictMapper, KbaseSysDict> implements IKbaseSysDictService {


    @Override
    public String getLabel(String type, String value) {
        KbaseSysDict kbaseSysDict = this.getOne(type, value);
        if (Objects.isNull(kbaseSysDict)) {
            return null;
        }
        return kbaseSysDict.getLabel();
    }

    @Override
    public String getLabelDefault(String type, String value, String s) {
        String label = this.getLabel(type, value);
        if (StringUtils.isBlank(label)) {
            return s;
        }
        return label;
    }

    @Override
    public boolean create(KbaseSysDict kbaseSysDict) {
        if (StringUtils.isAnyBlank(kbaseSysDict.getType(), kbaseSysDict.getValue())) {
            throw new MyException(50003, "类和值不可为空");
        }
        if (exist(kbaseSysDict.getType(), kbaseSysDict.getValue())) {
            throw new MyException(50003, "不可重复创建");
        }
        return super.save(kbaseSysDict);
    }

    @Override
    public boolean update(KbaseSysDict kbaseSysDict) {
        if (StringUtils.isAnyBlank(kbaseSysDict.getType(), kbaseSysDict.getValue())) {
            throw new MyException(50003, "类和值不可为空");
        }
        if (exist(kbaseSysDict.getType(), kbaseSysDict.getValue())) {
            throw new MyException(50003, "不可重复创建");
        }
        return super.updateById(kbaseSysDict);
    }

    @Override
    public boolean delete(String id) {
        return super.removeById(id);
    }

    @Override
    public Page<KbaseSysDict> page(KbaseSysDictVo kbaseSysDictVo) {
        QueryWrapper<KbaseSysDict> qw = Wrappers.query();
        qw.eq(StringUtils.isNotBlank(kbaseSysDictVo.getType()), "TYPE", kbaseSysDictVo.getType())
                .eq(StringUtils.isNotBlank(kbaseSysDictVo.getState()), "STATE", kbaseSysDictVo.getState());
        return super.page(kbaseSysDictVo.getPage(), qw);
    }

    @Override
    public List<KbaseSysDict> list(String type) {
        if (StringUtils.isBlank(type)) {
            return Lists.newArrayListWithExpectedSize(0);
        }
        QueryWrapper<KbaseSysDict> qw = Wrappers.query();
        qw.eq("TYPE", type);
        return super.list(qw);
    }

    @Override
    public KbaseSysDict getOne(String type, String value) {
        QueryWrapper<KbaseSysDict> qw = Wrappers.query();
        qw.eq("TYPE", type)
                .eq("VALUE", value);
        qw.last("limit 1");
        return super.getOne(qw);
    }

    private boolean exist(String type, String value) {
        QueryWrapper<KbaseSysDict> qw = Wrappers.query();
        qw.eq("type", type)
                .eq("value", value);
        qw.last("limit 1");
        return Objects.nonNull(super.getOne(qw));
    }
}
